﻿var jQ = jQuery(document);

jQuery.fn.pagerPukis = function(options){
	var options = $.extend({
		childClass: ''
	}, options);
	var childs = 0;
	childs = this.children('li.'+options.childClass).size();
	this.width(childs*30);
}
//ACCORDION DA PUKIS MODIFICADO
$.fn.accordion = function(options){
	var defaults ={
		timing: 700,
		type: 'heading',
		startOpen: true,
	}	
	var opts = $.extend(defaults, options);
	if(opts.type=='heading'){
		$('.arrow-holder').click(function(){
			if($(this).is('.hidden')){
				$(this).removeClass('hidden');
			}else{
				$(this).addClass('hidden');
			}
		});
		this.find('h3').each(function(){
			var isso =  $(this);
			isso.text(isso.text().replace(/[0-9]/g, ''));
			isso.text(isso.text().replace(/(/g, ''));
			isso.text(isso.text().replace(/)/g, ''));
			if($(this).next().is('ul')){
				isso.append('<span class="arrow-holder" style="cursor: pointer; display: block; float: right; height: 20px; text-align: right; width: 20px;"></span>');
			}
		});
		this.find('h4').each(function(){
			var isso =  $(this);
			if($(this).next().is('ul')){
				isso.append('<span class="arrow-holder" style="cursor: pointer; display: block; float: right; height: 20px; text-align: right; width: 20px;"></span>');
			}
		});
		this.find('h5').each(function(){
			var isso =  $(this);
			if($(this).next().is('ul')){
				isso.append('<span class="arrow-holder" style="cursor: pointer; display: block; float: right; height: 20px; text-align: right; width: 20px;"></span>');
			}
		});
		this.find('h3 span').click(function(){
			$(this).parent().next().not('h3, h4, h5, a').slideToggle(opts.timing);
		});
		this.find('h4 span').click(function(){
			$(this).parent().next().not('h3, h4, h5, a').slideToggle(opts.timing);
		});
		this.find('h5 span').click(function(){
			$(this).parent().next().not('h3, h4, h5, a').slideToggle(opts.timing);
		});
		if(!opts.startOpen){
			this.find('h3').each(function(){
				$(this).next().not('h3, h4, h5, a').hide();
			});
			this.find('h4').each(function(){
				$(this).next().not('h3, h4, h5, a').hide();
			});
			this.find('h5').each(function(){
				$(this).next().not('h3, h4, h5, a').hide();
			});
		}
	}
}
//FIM ACCORDION PUKIS MODIFICADO
var globalSystemCommon =
{
    init: function() { // Ap�s inicializa��o do script.
        globalSystemCommon.domManipulation.init();
    },
    ajaxStop: function() { // Ap�s o termino de cada requisi��o ajax.
        globalSystemCommon.domManipulation.addProfileLink();
    },
    windowOnload: function() { // Ap�s a inicializa��o do documento
        globalSystemCommon.loadPlugins.init();
    },
    loadPlugins: { // Definindo os plugins.
        init: function() {
            globalSystemCommon.loadPlugins.cuffon();
            globalSystemCommon.loadPlugins.productRoll();
        },
        cuffon: function() { // Slider das thumbnails
            Cufon.replace('#navigation ul li a, \
                #navigation,\
            ', { fontFamily: 'Chaparral Pro' });
        },
        productRoll: function() {
            $('.productroll .prateleira ul').each(function() {
                $(this).find('li').clone().appendTo( $(this) );//remover
                $(this).jQRoll();
            });
        }
    },
    domManipulation: { // Manipula��es do Dom.
        init: function() {
            globalSystemCommon.domManipulation.breadcrumbManip();
            globalSystemCommon.domManipulation.leftsizeManip();
        },
        breadcrumbManip: function() {
            $('#left-side .bread-crumb ul li:first').css({ 'display' : 'block' });
        },
        leftsizeManip: function() {
            var content = $('#content');
            var leftside = $('#left-side');
            if ( $(content).height() > $(leftside).height() ){
            
                $(leftside).css({   
                    'position' : 'absolute'
                });
            
            }
        },
        addProfileLink: function() {
            if ( $('#profile_link').length == 0 ){
                $('#lnkLogOut').after('<a id="profile_link" href="/Vitrine/MinhaConta/MinhaConta.aspx">Meu Perfil</a>');
            }
        }
    },
    methods: {
        init: function() {
            globalSystemCommon.methods.clickEvents();
        },
        clickEvents: function() {
            $('menu-navegue a').live('click', function(){
                globalSystemCommon.domManipulation.leftsizeManip();
            });        
        }
    }
};


jQuery(function() { globalSystemCommon.init(); jQ.ajaxStop(function() { globalSystemCommon.ajaxStop(); }); });
jQuery(window).load(function() { globalSystemCommon.windowOnload(); });

// Begin the plugin resposable of move the product list when the limit has passed.
$.fn.jQRoll = (function (options) {
    // Defining all customs variables.
    var options = $.extend({
        selectInstance : $(this),
        selectList : $(this).find("li"),
        selectListSize : $(this).find("li").size(),
        containerInstance : null,
        containerLimit : 4,
        moveSize : $(this).find('li:first-child').width() + 44,
        prevButton :  null,
        nextButton :  null,
        track : null
    },options);
    options.track = 0;
    var boxindex = 0;
    $(options.selectList).addClass('rollitems');
    
    // appending a container to the mathed ul.
    $(options.selectInstance).before('<div id="JQRollInstance"> </div>');
    // Matching the added container to a variable.
    options.containerInstance = $(options.selectInstance).prev();
    // Appending the mathed ul on the container.
    $(options.containerInstance).append(options.selectInstance);
    // Appeding the previously button.
    $(options.containerInstance).before('<a class="JQPrevButton disabled" href="javascript:void(0)">Prev</a>');
    // Appeding the next button
    $(options.containerInstance).after('<a class="JQNextButton" href="javascript:void(0)">Next</a>');
    // Finding the prev button.
    options.prevButton = $(options.containerInstance).parent().find(".JQPrevButton");
    // Finding the next button.
    options.nextButton = $(options.containerInstance).parent().find(".JQNextButton");
    
    if ( options.selectListSize <= options.containerLimit )
    {
        $(options.nextButton).addClass("disabled");
    }

    $(this).ajaxComplete(function(){ 
    
        if ( $(this).find(".rollitems").size() == 0 ){
            options.track = 0;
            boxindex = 0;
            options.selectListSize = $(this).find("li").size();
            $(this).find("li").addClass('rollitems');
            $(options.selectInstance).animate({ left : options.track }, 300);
            
        }
    
    });
    
    // When the prev buttton has clicked.
    $(options.nextButton).click(function () {
        if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
        
            options.track -= options.moveSize;
            boxindex += 1;
            $(options.selectInstance).animate({ left : options.track }, 500);
            if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
                $(options.nextButton).removeClass('disabled');
            }else{
                $(options.nextButton).addClass('disabled');
            }
            
            if( options.track < 0 ) {
                $(options.prevButton).removeClass('disabled');
            }else{
                $(options.prevButton).addClass('disabled');
            }
            
        }
        
    });
    
    // When the next button has clicked.
    $(options.prevButton).click(function () {
    
        //if( options.track < ( options.moveSize * options.selectListSize )  && options.track < ( options.moveSize * options.containerLimit )  ) {
        if( options.track < 0 ) {
            
            options.track += options.moveSize;
            boxindex -= 1;
            $(options.selectInstance).animate({ left : options.track }, 500);
            if( options.track < 0 ) {
                $(this).removeClass('disabled');
            }else{
                $(this).addClass('disabled');
            }
            
            if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
                $(options.nextButton).removeClass('disabled');
            }else{
                $(options.nextButton).addClass('disabled');
            }
        }
    
    });
    
});

// Begging the plugin resposable of make the select UI.
$.fn.jQSelect = (function(options) {
    
    // Defining all customs variables.
    var options = $.extend({
        selectInstance : $(this),
        optionList : $(this).find("option"),
        uiContainer : null,
        uiChild : null,
        uiItems : null,
        rotulo : "Selecione",
        callfun : (function() {
        
        }),
        selectfirst : true
    },options);
    
    $(this).each(function() {
    // Creating UI the element.
    $(this).before('<div class="jQcustomselect" ><a title="Clique aqui para selecionar uma op&ccedil;&atilde;o" class="jqselectlabel" href="javascript:void(0)">' + options.rotulo + '</a></div>');
    // Matching the added object.
    options.uiContainer = $(this).prev();
    // Creating UI inside the element.
    $(options.uiContainer).append('<ul class="ui jQSelect" > </ul>')
    // Matching the added object.
    options.uiChild = $(options.uiContainer).find(".jQSelect");
    
    // Travelsing all option's.
    $(options.optionList).each(function() {
        // Appending the option content on a ul created previously.
        $(options.uiChild).append('<li><a href="javascript:void(0)">' + $(this).html() + '</a></li>');
    });
    
    // Matching the added items.
    options.uiItems = $(options.uiContainer).find("li");
    
    // When the list item has clicked.
    $(options.uiItems).click(function() {
        // Setting up the value to the original select element.
        $(options.selectInstance).val($(options.optionList[ $(this).index($(options.optionList).selection) ]).val());
        // Setting up the select texto to the label.
        $(options.uiContainer).children('.jqselectlabel').html($(options.optionList[ $(this).index($(options.optionList).selection) ]).html());
        $(options.optionList[ $(this).index($(options.optionList).selection) ]).click();

        // hiding the list.
        $(options.uiChild).hide();
        $(options.selectInstance).trigger("change");
        
        options.callfun(); 
        
    });
    
    // When then label has clicked.
    $(options.uiContainer).children('.jqselectlabel').click(function() {
        // Listing the select options.
        $(options.uiChild).slideToggle();
    
    });
    
    
    });
    if ( options.selectfirst == true ){
        $(options.uiItems).each(function(){
            if ( $(this).find("a").html() == $(options.selectInstance).find('option:selected').html() ){
               $(options.uiContainer).children('.jqselectlabel').html( $(this).find("a").html() );
            }
        });
    }
});

$(document).ready(function() {

    $(".categoria-principal").mouseover(function(){
        if($(this).find(".shadow-box").length == 0){
            $(this).append("<div class='shadow-box'></div>");
        }
        var boxHeight = $(this).find(" > .subcategoria-principal").height();
            $(this).find(" > .shadow-box").css({"border-right": "5px solid #999", "border-bottom": "5px solid #999", "opacity":"0.5", "height": boxHeight});
    });
        
    $(".categoria-principal .subcategoria-principal li").mouseover(function(){
       if($(this).find(".shadow-box").length == 0){
            $(this).find('.submenu-subcategoria-principal').after("<div class='shadow-box'></div>");        
       }
       var boxHeight = $(this).find(".submenu-subcategoria-principal").height();
       $(this).find(".shadow-box").css({"border-right": "5px solid #999", "border-bottom": "5px solid #999", "opacity":"0.5", "height": boxHeight});

    });
	if($('#entrance .loginmsg a').text()== 'Login')
	{
	    $('#entrance .loginmsg a').clone().appendTo('#entrance .loginmsg em');
	    $('#entrance .loginmsg a:last').text('CADASTRE-SE');
	    $('#entrance .loginmsg a:last').css('margin-right', 5);
	}
	$('ul.ui-autocomplete').appendTo('#header');

	$(document).ajaxComplete(function(){
		$('ul.pages:eq(1)').pagerPukis({
			childClass: 'page-number'
		});
	});
	$('.menu-departamento').accordion();
	$('.busca').children('span').hide()
});
	